--Function of adding new guid column
create or replace procedure meta_add_guid_column(
    --Table id
    a_table_id raw,
    --Column id
    a_column_id raw,
    --Column name
    a_column_name varchar2)
as
    --Table name
    a_table_name varchar2(26);
begin
    p_check_col_not_exists(a_table_id, a_column_id, a_column_name);

    a_table_name :=  p_get_table_name(a_table_id);
    
    --Changing the metadata
    insert into meta_guid_columns(column_id, table_id, column_name,
        order_index, customization_level)
    values (a_column_id, a_table_id, a_column_name,
        p_generate_order_index(a_table_id), 1);
        
    --Dropping table view
    p_drop_view_if_exists(a_table_name);
         
    execute immediate 'alter table tbl_' 
    || a_table_name
    || ' add ' || a_column_name
    || ' raw(16)';
         
    --Creating table view
    p_create_table_view(a_table_id);

end;

--go

--Function of setting column default value
create or replace procedure meta_set_guid_default(
    --Column id
    a_column_id raw,
    --Default value
    a_default_value raw)
as
    --Table id
    a_table_id raw(16);
    --Table name
    a_table_name varchar2(26);
    --Column name
    a_column_name varchar2(26);
    --Customization level
    a_customization_level number(10);
begin
    p_check_col_type(a_column_id, 'meta_guid_columns', 'guid');
    
    --Reading column properties
    select table_id, column_name, customization_level
    into a_table_id, a_column_name, a_customization_level
    from meta_guid_columns
    where column_id = a_column_id;
    
    --If this is a system column
    if a_customization_level = 0 then
        raise_application_error(-20008,
                'Changing guid system column default value is forbidden (column_id = '
                || a_column_id
                || ') (#error_code = 20008)');
    end if;
    
    a_table_name := p_get_table_name(a_table_id);
    
    if a_default_value is not null then
        execute immediate 'alter table tbl_' 
        || a_table_name
        || ' modify (' || a_column_name
        || ' default ''' || a_default_value || ''')';
    else
        execute immediate 'alter table tbl_' 
        || a_table_name
        || ' modify (' || a_column_name
        || ' default null)';
    end if;
    
    --Updating the metadata
    update meta_guid_columns
    set default_value = a_default_value
    where column_id = a_column_id;
end;

--go